package com.wxs.shiro.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.wxs.common.bean.UserLoginInfoBean;
import com.wxs.common.service.LoginService;
import com.wxs.common.utils.JsonResult;

/**
 * 登录
 * 
 * @author admin
 *
 */
@Controller
public class LoginController {
	@Autowired
	private LoginService loginService;

	@RequestMapping("/login")
	public ModelAndView login() {
		ModelAndView modelAndView = new ModelAndView("login");
		return modelAndView;
	}

	@ResponseBody
	@RequestMapping("/login/doLogin")
	public String doLogin(HttpServletRequest request, HttpServletResponse response) throws IOException {
		String username = request.getParameter("username");
		String password = request.getParameter("password");

		String vcode = request.getParameter("vcode");

		JsonResult jsonResult = loginService.login(username, password, vcode, false);
		if (jsonResult.getState() == 0) {
			UserLoginInfoBean userLoginInfoBean = (UserLoginInfoBean) jsonResult.getData();

			HttpSession session = request.getSession();

			session.setAttribute(UserLoginInfoBean.userSessionName, userLoginInfoBean);

			jsonResult.setData(null);
			return jsonResult.toString();
		} else {
			return jsonResult.toString();

		}
	}
}
